package com.hanhai.material.utils.jiandaoyun;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.device.entity.*;
import com.hanhai.material.device.service.*;
import com.hanhai.material.model.productdata.*;
import com.hanhai.material.scheduling.entity.SysProductPeople;
import com.hanhai.material.scheduling.service.SysProductPeopleService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.relation.RelationManager;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.management.Query;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

import static com.hanhai.material.device.entity.table.SysBadRecordTableDef.SYS_BAD_RECORD;
import static com.hanhai.material.device.entity.table.SysPlcTableDef.SYS_PLC;
import static com.hanhai.material.device.entity.table.SysProductRecordDetentionRecordTableDef.SYS_PRODUCT_RECORD_DETENTION_RECORD;
import static com.hanhai.material.device.entity.table.SysProductRecordDetentionTableDef.SYS_PRODUCT_RECORD_DETENTION;
import static com.hanhai.material.device.entity.table.SysProductRecordIntoTableDef.SYS_PRODUCT_RECORD_INTO;
import static com.hanhai.material.scheduling.entity.table.SysProductPeopleTableDef.SYS_PRODUCT_PEOPLE;
import static com.hanhai.material.utils.TimeUtils.fromDateToString;
import static com.mybatisflex.core.query.QueryMethods.*;

/**
 * @author Administrator
 */
@Component
@Log4j2
public class PushToJianDaoYunUtil {

    @Value("${jiandaoyun.Authorization}")
    private String authorization;
    @Value("${jiandaoyun.url}")
    private String baseUrl;

    @Autowired
    SysProductRecordIntoService sysProductRecordIntoService;

    @Autowired
    SysProductRecordDetentionService sysProductRecordDetentionService;

    @Autowired
    SysBadRecordService sysBadRecordService;

    @Autowired
    SysProductPeopleService sysProductPeopleService;

    @Autowired
    SysPlcService sysPlcService;

    @Autowired
    SysProductRecordDetentionRecordService sysProductRecordDetentionRecordService;

    public void pushToJianDaoYun() {
        RestTemplate restTemplate = new RestTemplate();
        String url = baseUrl + "/v5/app/entry/data/batch_create";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        headers.set("Authorization", authorization);
        ProductTodayData productTodayData = this.pushProductTodayData();
        productTodayData.setEntry_id("6389623bb23e54000a1f5429");
        productTodayData.setApp_id("6381bdaf6af134000ad6629f");
        productTodayData.setOp("batch_create");
        if (productTodayData.getData_list().size() > 0) {
            HttpEntity<ProductTodayData> httpEntity = new HttpEntity<>(productTodayData, headers);
            ResponseEntity<Object> responseEntity = restTemplate.postForEntity(url, httpEntity, Object.class);
            log.error("ENtity:{}", JSONObject.toJSONString(httpEntity));
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(responseEntity.getBody());
            log.error("JSONOBJECT:{}", jsonObject);
            JSONArray jsonArray = jsonObject.getJSONArray("success_ids");
            log.error("JSON:{}", jsonArray);
        }

    }

    public String pushToJianDaoYunSingle() {
        RestTemplate restTemplate = new RestTemplate();
        String url = baseUrl + "/v5/app/entry/data/create";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        headers.set("Authorization", authorization);
        ProductTodayDataSingle productTodayDataSingle = this.pushProductTodayDataSingle();
        productTodayDataSingle.setEntry_id("6389623bb23e54000a1f5429");
        productTodayDataSingle.setApp_id("6381bdaf6af134000ad6629f");
        productTodayDataSingle.setOp("data_create");
        HttpEntity<ProductTodayDataSingle> httpEntity = new HttpEntity<>(productTodayDataSingle, headers);
        ResponseEntity<Object> responseEntity = restTemplate.postForEntity(url, httpEntity, Object.class);
        log.error("ENtity:{}", JSONObject.toJSONString(httpEntity));
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(responseEntity.getBody());
        log.error("JSONOBJECT:{}", JSONObject.toJSONString(jsonObject));
        return jsonObject.getJSONObject("data").getString("_id");


    }

    /**
     *
     *推送到简道云
     * @param sysProductRecordInto
     * @return
     */
    public String pushToJianDaoYunSingle1(SysProductRecordInto sysProductRecordInto) {
        RestTemplate restTemplate = new RestTemplate();
        String url = baseUrl + "/v5/app/entry/data/create";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        headers.set("Authorization", authorization);
        ProductTodayDataSingle productTodayDataSingle = this.pushProductTodayDataSingle1(sysProductRecordInto);
        productTodayDataSingle.setEntry_id("6389623bb23e54000a1f5429");
        productTodayDataSingle.setApp_id("6381bdaf6af134000ad6629f");
        productTodayDataSingle.setOp("data_create");
        HttpEntity<ProductTodayDataSingle> httpEntity = new HttpEntity<>(productTodayDataSingle, headers);
        ResponseEntity<Object> responseEntity = restTemplate.postForEntity(url, httpEntity, Object.class);
        log.error("ENtity:{}", JSONObject.toJSONString(httpEntity));
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(responseEntity.getBody());
        log.error("JSONOBJECT:{}", JSONObject.toJSONString(jsonObject));
        return jsonObject.getJSONObject("data").getString("_id");


    }

    /**
     * 更新数据
     * @param sysProductRecordInto
     * @return
     */
    public String updateToJianDaoYunSingle1(SysProductRecordInto sysProductRecordInto) {
        RestTemplate restTemplate = new RestTemplate();
        String url = baseUrl + "/v5/app/entry/data/update";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        headers.set("Authorization", authorization);
        UpdateProductTodayDataSingle updateProductTodayDataSingle = this.updteProductTodayDataSingle1(sysProductRecordInto);
        log.error("Update:{}", JSONObject.toJSONString(updateProductTodayDataSingle));
        updateProductTodayDataSingle.setEntry_id("6389623bb23e54000a1f5429");
        updateProductTodayDataSingle.setApp_id("6381bdaf6af134000ad6629f");
        updateProductTodayDataSingle.setOp("data_update");
        updateProductTodayDataSingle.setData_id(sysProductRecordInto.getDataId());
        HttpEntity<UpdateProductTodayDataSingle> httpEntity = new HttpEntity<>(updateProductTodayDataSingle, headers);
        ResponseEntity<Object> responseEntity = restTemplate.postForEntity(url, httpEntity, Object.class);
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(responseEntity.getBody());
        return jsonObject.getJSONObject("data").getString("_id");


    }

    public ProductTodayData pushProductTodayData() {
        RelationManager.addExtraConditionParam("productDate", "2024-03-13");
        RelationManager.addExtraConditionParam("status", 2);
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_INTO.ID.eq("b701860b-da94-4851-8f35-c707706102b0"));
        SysProductRecordInto productRecordInto = this.sysProductRecordIntoService.getMapper().selectOneWithRelationsByQuery(queryWrapper);
        SysProductRecordShift productRecordShift = productRecordInto.getProductRecordShift();
        SysWorkPlan workPlan = productRecordShift.getWorkPlan();
        SysProductInfo productInfo = productRecordInto.getProductInfo();
        Date productDate = productRecordInto.getProductDate();
        String machineName = productRecordInto.getMachineName();
        ProductTodayData productTodayData = new ProductTodayData();
        List<Data> dataList = new ArrayList<>();
        Data data = new Data();
        /*数据Key*/
        Key key = new Key();
        key.setValue(fromDateToString("yyyyMMdd", productDate) + machineName + workPlan.getMoldNo() + productRecordInto.getProductId() + productRecordInto.getShift() + productRecordInto.getShiftCode());
        data.setKey(key);

        /*员工姓名*/
        User_name user_name = new User_name();
        user_name.setValue(productRecordInto.getDingId());
        data.setUser_name(user_name);

        /*机台*/
        Machine_name machine_name = new Machine_name();
        machine_name.setValue(productRecordInto.getMachineName());
        data.setMachine_name(machine_name);

        /*模具*/
        Mold_name mold_name = new Mold_name();
        mold_name.setValue(workPlan.getMoldNo());
        data.setMold_name(mold_name);

        /*模穴数*/
        Mold_num mold_num = new Mold_num();
        mold_num.setValue((int) workPlan.getMoldNumber());
        data.setMold_num(mold_num);

        /*总数*/
        Total_count total_count = new Total_count();
        total_count.setValue(productRecordShift.getTotalCount());
        data.setTotal_count(total_count);


        Product_id product_id = new Product_id();
        product_id.setValue(productRecordInto.getProductId());
        data.setProduct_id(product_id);



        /*生产日期*/
        Product_date product_date = new Product_date();
        product_date.setValue(productDate);
        data.setProduct_date(product_date);

        /*开始时间*/
        Product_start_time product_start_time = new Product_start_time();
        product_start_time.setValue(productRecordShift.getActualStartTime());
        data.setProduct_start_time(product_start_time);

        /*结束时间*/
        Product_end_time product_end_time = new Product_end_time();
        product_end_time.setValue(productRecordShift.getActualEndTime());
        data.setProduct_end_time(product_end_time);

        long times = productRecordShift.getActualEndTime().getTime() - productRecordShift.getActualStartTime().getTime();
        double hours = times / (60 * 60 * 1000);
        BigDecimal bigDecimal = BigDecimal.valueOf(hours);
        double hour = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        if (hour >= 5) {
            hour = hour - 0.5;
        }
        hour=hour==0?11.5:hour;

        Actual_work_time_num actual_work_time_num = new Actual_work_time_num();
        actual_work_time_num.setValue(hour);
        data.setActual_work_time_num(actual_work_time_num);

        /*班别*/
        Shift shift = new Shift();
        shift.setValue(productRecordInto.getShiftCode());
        data.setShift(shift);

        Is_auto is_auto = new Is_auto();
        is_auto.setValue("全自动");
        data.setIs_auto(is_auto);

        if (productInfo != null) {
            Customer_name customer_name = new Customer_name();
            customer_name.setValue(productInfo.getCustomerName());
            data.setCustomer_name(customer_name);

            Standard_recycle standard_recycle = new Standard_recycle();
            standard_recycle.setValue(productInfo.getStandardCycle().intValue());
            data.setStandard_recycle(standard_recycle);

            Machine_type_name machine_type_name = new Machine_type_name();
            machine_type_name.setValue(productInfo.getMachineType());
            data.setMachine_type_name(machine_type_name);

            Machine_type_short_name machine_type_short_name = new Machine_type_short_name();
            machine_type_short_name.setValue(productInfo.getMachineType() + "-" + productInfo.getMachineItem());
            data.setMachine_type_short_name(machine_type_short_name);

            int standardProductNum = (int) ((hour * 60 * 60 * workPlan.getMoldNumber()) / productInfo.getStandardCycle());
            Standard_num standard_num = new Standard_num();
            standard_num.setValue(standardProductNum);
            data.setStandard_num(standard_num);

            Input_quantity input_quantity = new Input_quantity();
            if (productRecordShift.getTotalNumPeople() != null) {
                input_quantity.setValue(productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionNum() + productRecordInto.getBadNumPeople() + productRecordInto.getWillProcessedNum() + productRecordInto.getNowTailBoxCount());
            }
            data.setInput_quantity(input_quantity);

            Aux_product_capacity aux_product_capacity = new Aux_product_capacity();
            int capacity = (int) ((60 * 60) / productInfo.getStandardCycle());
            aux_product_capacity.setValue(capacity);
            data.setAux_product_capacity(aux_product_capacity);
            if (productRecordShift.getTotalNumPeople() != null) {
                double rate = productRecordShift.getTotalNumPeople() / standardProductNum;
                BigDecimal rateBigDecimal = new BigDecimal(rate);
                double rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                Aux_achievement_rate aux_achievement_rate = new Aux_achievement_rate();
                aux_achievement_rate.setValue(rateN);
                data.setAux_achievement_rate(aux_achievement_rate);
            }


        }

        Good_num good_num = new Good_num();

        good_num.setValue(productRecordInto.getGoodNumPeople());
        data.setGood_num(good_num);

        Aux_bad_rate aux_bad_rate = new Aux_bad_rate();
        int total = productRecordInto.getGoodNumPeople() + productRecordInto.getBadNumPeople();
        double rate = (double) productRecordInto.getBadNumPeople() / total;
        BigDecimal rateBigDecimal = new BigDecimal(rate);
        double rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        aux_bad_rate.setValue(rateN);
        data.setAux_bad_rate(aux_bad_rate);

        /*当日尾箱*/
        Tail_box_num tail_box_num = new Tail_box_num();
        tail_box_num.setValue(productRecordShift.getNowTailBoxCount());
        data.setTail_box_num(tail_box_num);
        /*前日尾箱*/
        Last_tail_box_num last_tail_box_num = new Last_tail_box_num();
        last_tail_box_num.setValue(productRecordShift.getLastTailBoxCount());
        data.setLast_tail_box_num(last_tail_box_num);

//        Detention_num detention_num=new Detention_num();
//        detention_num.setValue(productRecordShift.);

        queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.PRODUCT_DATE.eq(productDate));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.NUM.gt(0));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.select(SYS_PRODUCT_RECORD_DETENTION.PRODUCT_DATE, sum(SYS_PRODUCT_RECORD_DETENTION.NUM).as("num"), SYS_PRODUCT_RECORD_DETENTION.TYPE);
        queryWrapper.groupBy(SYS_PRODUCT_RECORD_DETENTION.TYPE);
        List<SysProductRecordDetention> productRecordDetentionList = this.sysProductRecordDetentionService.list(queryWrapper);
        for (SysProductRecordDetention productRecordDetention : productRecordDetentionList) {
            switch (productRecordDetention.getType()) {
                case "待加工":
                    Pending_into_num pending_into_num = new Pending_into_num();
//                    pending_into_num.setValue();
                    break;
                case "扣留":
                    Detention_num detention_num = new Detention_num();
                    detention_num.setValue(productRecordDetention.getNum());
                    data.setDetention_num(detention_num);
                    break;
                default:
                    break;
            }
        }
        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_BAD_RECORD.CREATE_TIME.between(productRecordInto.getActualStartTime(),productRecordInto.getActualEndTime()));
        queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
        queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productDate));
        queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.select(SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
        queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
        List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
        Base_bad base_bad = new Base_bad();
        List<Value1> value1List = new ArrayList<>();
        Process_bad process_bad = new Process_bad();
        List<ProcessBadValue> processBadValueList = new ArrayList<>();
        Manufactue_bad manufactue_bad = new Manufactue_bad();
        List<ManufactureBadValue> manFactureBadValueList = new ArrayList<>();
        int openBadNum = 0;
        int manufactureBadNum = 0;
        int processBadNum = 0;
        int productBadNum = 0;
        int totalBadNum = 0;
        for (SysBadRecord badRecord : badRecordList) {
            totalBadNum += badRecord.getNum();
            switch (badRecord.getReasonType()) {
                case "开机不良":
                    openBadNum += badRecord.getNum();
                    break;
                case "制程不良":
                    ManufactureBadValue manufactureBadValue = new ManufactureBadValue();
                    Manufacture_bad_reason manufacture_bad_reason = new Manufacture_bad_reason();
                    manufacture_bad_reason.setValue(badRecord.getReasonName());
                    manufactureBadValue.setManufactue_bad_reason(manufacture_bad_reason);

                    Manufacture_bad_num manufacture_bad_num = new Manufacture_bad_num();
                    manufacture_bad_num.setValue(badRecord.getNum());
                    manufactureBadValue.setManufactue_bad_num(manufacture_bad_num);
                    manFactureBadValueList.add(manufactureBadValue);
                    manufactureBadNum += badRecord.getNum();
                    break;

                case "加工不良":
                    processBadNum += badRecord.getNum();
                    ProcessBadValue processBadValue = new ProcessBadValue();
                    Process_bad_reason process_bad_reason = new Process_bad_reason();
                    process_bad_reason.setValue(badRecord.getReasonName());
                    processBadValue.setProcess_bad_reason(process_bad_reason);

                    Process_bad_num process_bad_num = new Process_bad_num();
                    process_bad_num.setValue(badRecord.getNum());
                    processBadValue.setProcess_bad_num(process_bad_num);
                    processBadValueList.add(processBadValue);
                    break;
                default:

                    Value1 value1 = new Value1();
                    Base_bad_reason base_bad_reason = new Base_bad_reason();
                    base_bad_reason.setValue(badRecord.getReasonType());
                    value1.setBase_bad_reason(base_bad_reason);

                    Base_bad_num base_bad_num = new Base_bad_num();
                    base_bad_num.setValue(badRecord.getNum());
                    value1.setBase_bad_num(base_bad_num);
                    value1List.add(value1);
                    break;
            }

        }
        Total_bad_num total_bad_num = new Total_bad_num();
        total_bad_num.setValue(totalBadNum);
        data.setTotal_bad_num(total_bad_num);

        base_bad.setValue(value1List);
        manufactue_bad.setValue(manFactureBadValueList);
        process_bad.setValue(processBadValueList);
        data.setBase_bad(base_bad);
        data.setManufactue_bad(manufactue_bad);
        data.setProcess_bad(process_bad);

        Aux_open_machine_bad aux_open_machine_bad = new Aux_open_machine_bad();
        aux_open_machine_bad.setValue(openBadNum);
        data.setAux_open_machine_bad(aux_open_machine_bad);

        Aux_process_bad aux_process_bad = new Aux_process_bad();
        aux_process_bad.setValue(manufactureBadNum);
        data.setAux_process_bad(aux_process_bad);

        Aux_product_bad aux_product_bad = new Aux_product_bad();
        aux_product_bad.setValue(productBadNum);
        data.setAux_product_bad(aux_product_bad);

        Aux_shift aux_shift = new Aux_shift();
        aux_shift.setValue(productRecordInto.getShiftCode());
        data.setAux_shift(aux_shift);

        Aux_effective_hours aux_effective_hours = new Aux_effective_hours();
        aux_effective_hours.setValue(hour - (productRecordInto.getAdjustMachineDuration() / 60));
        data.setAux_effective_hours(aux_effective_hours);

        Aux_product_name aux_product_name = new Aux_product_name();
        aux_product_name.setValue(productRecordInto.getProductName());
        data.setAux_product_name(aux_product_name);

        Today_into_num today_into_num = new Today_into_num();

        today_into_num.setValue(productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionToGoodNum() + productRecordInto.getWillProcessedToGoodNum());

        data.setToday_into_num(today_into_num);

        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_PLAN_ID.eq(productRecordInto.getWorkPlanId()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(productRecordInto.getMachineName()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_DATE.eq(fromDateToString("yyyy-MM-dd", productDate)));
        queryWrapper.select(distinct(SYS_PRODUCT_PEOPLE.JOB_NO), SYS_PRODUCT_PEOPLE.USER_NAME);
        log.error("员工：{}", queryWrapper.toSQL());
        List<SysProductPeople> productPeopleList = this.sysProductPeopleService.list(queryWrapper);
        log.error("ProductPeopleList:{}", JSONObject.toJSONString(productPeopleList));
        int i = 0;
        People people = new People();
        List<PeopleInfo> peopleInfoList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        StringJoiner userNameList = new StringJoiner(",");
        StringJoiner jobNoList = new StringJoiner(",");
        for (SysProductPeople productPeople : productPeopleList) {
            userNameList.add(productPeople.getUserName());
            PeopleInfo peopleInfo = new PeopleInfo();
            i++;
            People_index people_index = new People_index();
            people_index.setValue(i);
            peopleInfo.setPeople_index(people_index);
            userNameList.add(productPeople.getUserName());
            jobNoList.add(productPeople.getJobNo());
            People_job_no people_job_no = new People_job_no();
            people_job_no.setValue(productPeople.getJobNo());
            peopleInfo.setPeople_job_no(people_job_no);

            People_user_name people_user_name = new People_user_name();
            people_user_name.setValue(productPeople.getUserName());
            peopleInfo.setPeople_user_name(people_user_name);
            peopleInfoList.add(peopleInfo);

        }
        people.setValue(peopleInfoList);
        data.setPeople(people);

        User_name1 user_name1 = new User_name1();
        user_name1.setValue(nameList);
        data.setUser_name1(user_name1);

        Remark_name remark_name = new Remark_name();
        remark_name.setValue(userNameList.toString());
        data.setRemark_name(remark_name);
        int peopleSize = productPeopleList.size();
        Aux_people_num aux_people_num = new Aux_people_num();
        aux_people_num.setValue(peopleSize);
        data.setAux_people_num(aux_people_num);

        People_num people_num = new People_num();
        people_num.setValue(String.valueOf(peopleSize));
        data.setPeople_num(people_num);

        Aux_product_time aux_product_time = new Aux_product_time();
        aux_product_time.setValue(productDate.getTime());
        data.setAux_product_time(aux_product_time);

        Line_manager_job_no line_manager_job_no = new Line_manager_job_no();
        line_manager_job_no.setValue(productRecordInto.getCreateUserId());
        data.setLine_manager_job_no(line_manager_job_no);

        Line_manager_name line_manager_name = new Line_manager_name();
        line_manager_name.setValue(productRecordInto.getCreateUserName());
        data.setLine_manager_name(line_manager_name);
//        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PLC.MACHINE_NAME.eq(productRecordInto.getMachineName()));
//        queryWrapper.select(avg(SYS_PLC.AVERAGE_PERIOD).as("average_period"));
//        queryWrapper.where(SYS_PLC.CREATE_TIME.between(productRecordShift.getActualStartTime(), productRecordShift.getActualEndTime()));
//        SysPlc sysPlc = this.sysPlcService.getOne(queryWrapper);
//        if (sysPlc != null) {
//            Actual_cycle actual_cycle = new Actual_cycle();
//            actual_cycle.setValue((double) sysPlc.getAveragePeriod() / 1000);
//            data.setActual_cycle(actual_cycle);
//        }
        int total1=productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionNum() + productRecordInto.getBadNumPeople() + productRecordInto.getWillProcessedNum() + productRecordInto.getNowTailBoxCount();

        if (total1>0) {
            Actual_cycle actual_cycle = new Actual_cycle();
            int actual= (int) (hour*60*60/total1);
            if(actual>100){
                actual=35;
            }
            actual_cycle.setValue(actual);
            data.setActual_cycle(actual_cycle);
        }
        Remark remark = new Remark();
        remark.setValue(productRecordInto.getSummary());
        data.setRemark(remark);
        dataList.add(data);
        productTodayData.setData_list(dataList);
        return productTodayData;
    }

    public ProductTodayDataSingle pushProductTodayDataSingle() {
//        RelationManager.addExtraConditionParam("productDate", "2024-03-13");
//        RelationManager.addExtraConditionParam("status", 2);
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_INTO.ID.eq("a5d268c7-cb72-4c4d-8a31-7419358338b7"));
        SysProductRecordInto productRecordInto = this.sysProductRecordIntoService.getMapper().selectOneWithRelationsByQuery(queryWrapper);
        SysProductRecordShift productRecordShift = productRecordInto.getProductRecordShift();
        SysWorkPlan workPlan = productRecordShift.getWorkPlan();
        SysProductInfo productInfo = productRecordInto.getProductInfo();
        log.error("ProductRecord:{}", JSONObject.toJSONString(productRecordShift));
        log.error("ProductInfo:{}", JSONObject.toJSONString(productInfo));
        Date productDate = productRecordInto.getProductDate();
        String machineName = productRecordInto.getMachineName();

        ProductTodayDataSingle productTodayDataSingle = new ProductTodayDataSingle();

        Data data = new Data();
        /*数据Key*/
        Key key = new Key();
        key.setValue(fromDateToString("yyyyMMdd", productDate) + machineName + workPlan.getMoldNo() + productRecordInto.getProductId() + productRecordInto.getShift() + productRecordInto.getShiftCode());
        data.setKey(key);

        /*员工姓名*/
        User_name user_name = new User_name();
        user_name.setValue("SYSTEM");
        data.setUser_name(user_name);

        /*机台*/
        Machine_name machine_name = new Machine_name();
        machine_name.setValue(productRecordInto.getMachineName());
        data.setMachine_name(machine_name);

        /*模具*/
        Mold_name mold_name = new Mold_name();
        mold_name.setValue(workPlan.getMoldNo());
        data.setMold_name(mold_name);

        /*模穴数*/
        Mold_num mold_num = new Mold_num();
        mold_num.setValue((int) workPlan.getMoldNumber());
        data.setMold_num(mold_num);

        /*总数*/
        Total_count total_count = new Total_count();
        total_count.setValue(productRecordShift.getTotalCount());
        data.setTotal_count(total_count);


        Product_id product_id = new Product_id();
        product_id.setValue(productRecordInto.getProductId());
        data.setProduct_id(product_id);



        /*生产日期*/
        Product_date product_date = new Product_date();
        product_date.setValue(productDate);
        data.setProduct_date(product_date);

        /*开始时间*/
        Product_start_time product_start_time = new Product_start_time();
        product_start_time.setValue(productRecordShift.getActualStartTime());
        data.setProduct_start_time(product_start_time);

        /*结束时间*/
        Product_end_time product_end_time = new Product_end_time();
        product_end_time.setValue(productRecordShift.getActualEndTime());
        data.setProduct_end_time(product_end_time);
        long times = productRecordShift.getActualEndTime().getTime() - productRecordShift.getActualStartTime().getTime();
        double hours = times / (60 * 60 * 1000);
        BigDecimal bigDecimal = BigDecimal.valueOf(hours);
        double hour = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        if (hour >= 5) {
            hour = hour - 0.5;
        }
        hour=hour==0?11.5:hour;
        Actual_work_time_num actual_work_time_num = new Actual_work_time_num();
        actual_work_time_num.setValue(hour);
        data.setActual_work_time_num(actual_work_time_num);
        /*班别*/
        Shift shift = new Shift();
        shift.setValue(productRecordInto.getShiftCode());
        data.setShift(shift);

        Is_auto is_auto = new Is_auto();
        is_auto.setValue("全自动");
        data.setIs_auto(is_auto);
        queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.PRODUCT_DATE.eq(productDate));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.NUM.gt(0));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.select(SYS_PRODUCT_RECORD_DETENTION.PRODUCT_DATE, sum(SYS_PRODUCT_RECORD_DETENTION.NUM).as("num"), SYS_PRODUCT_RECORD_DETENTION.TYPE);
        queryWrapper.groupBy(SYS_PRODUCT_RECORD_DETENTION.TYPE);
        List<SysProductRecordDetention> productRecordDetentionList = this.sysProductRecordDetentionService.list(queryWrapper);
        int detentionNum = 0;
        for (SysProductRecordDetention productRecordDetention : productRecordDetentionList) {
            detentionNum += productRecordDetention.getNum();
        }
        Detention_num detention_num = new Detention_num();
        detention_num.setValue(detentionNum);
        data.setDetention_num(detention_num);

        if (productInfo != null) {
            Customer_name customer_name = new Customer_name();
            customer_name.setValue(productInfo.getCustomerName());
            data.setCustomer_name(customer_name);

            Standard_recycle standard_recycle = new Standard_recycle();
            standard_recycle.setValue(productInfo.getStandardCycle().intValue());
            data.setStandard_recycle(standard_recycle);

            Machine_type_name machine_type_name = new Machine_type_name();
            machine_type_name.setValue(productInfo.getMachineType());
            data.setMachine_type_name(machine_type_name);

            Machine_type_short_name machine_type_short_name = new Machine_type_short_name();
            machine_type_short_name.setValue(productInfo.getMachineType() + "-" + productInfo.getMachineItem());
            data.setMachine_type_short_name(machine_type_short_name);

            int standardProductNum = (int) ((hour * 60 * 60 * workPlan.getMoldNumber()) / productInfo.getStandardCycle());
            Standard_num standard_num = new Standard_num();
            standard_num.setValue(standardProductNum);
            data.setStandard_num(standard_num);

            Input_quantity input_quantity = new Input_quantity();
            if (productRecordShift.getTotalNumPeople() != null) {
                input_quantity.setValue(productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionNum() + productRecordInto.getBadNumPeople() + productRecordInto.getWillProcessedNum() + productRecordInto.getNowTailBoxCount());
            }
            data.setInput_quantity(input_quantity);

            Aux_product_capacity aux_product_capacity = new Aux_product_capacity();
            int capacity = (int) ((60 * 60) / productInfo.getStandardCycle());
            aux_product_capacity.setValue(capacity);
            data.setAux_product_capacity(aux_product_capacity);
            int handleInto = 0;

            handleInto = productRecordInto.getTotalHandleInto();

            double rate = (double) (productRecordShift.getGoodNumPeople() + productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum() + productRecordInto.getNowTailBoxCount()) / standardProductNum;
            BigDecimal rateBigDecimal = new BigDecimal(rate);
            double rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            Aux_achievement_rate aux_achievement_rate = new Aux_achievement_rate();
            aux_achievement_rate.setValue(rateN);
            data.setAux_achievement_rate(aux_achievement_rate);

        }

        Good_num good_num = new Good_num();
        good_num.setValue(productRecordInto.getGoodNumPeople());
        data.setGood_num(good_num);

        Aux_bad_rate aux_bad_rate = new Aux_bad_rate();
        double rate = (double) productRecordInto.getBadNumPeople() / productRecordShift.getTotalNumPeople();
        BigDecimal rateBigDecimal = new BigDecimal(rate);
        double rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        aux_bad_rate.setValue(rateN);
        data.setAux_bad_rate(aux_bad_rate);

        /*当日尾箱*/
        Tail_box_num tail_box_num = new Tail_box_num();
        tail_box_num.setValue(productRecordShift.getNowTailBoxCount());
        data.setTail_box_num(tail_box_num);
        /*前日尾箱*/
        Last_tail_box_num last_tail_box_num = new Last_tail_box_num();
        last_tail_box_num.setValue(productRecordShift.getLastTailBoxCount());
        data.setLast_tail_box_num(last_tail_box_num);

//        Detention_num detention_num=new Detention_num();
//        detention_num.setValue(productRecordShift.);


        queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.PRODUCT_RECORD_INTO_ID.eq(productRecordInto.getId()));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.TYPE.eq("良品"));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.NUM.gt(0));
        queryWrapper.groupBy(SYS_PRODUCT_RECORD_DETENTION_RECORD.PRODUCT_DATE);
        List<SysProductRecordDetentionRecord> sysProductRecordDetentionRecordList = this.sysProductRecordDetentionRecordService.list(queryWrapper);
        Pending_into_num pending_into_num = new Pending_into_num();
        List<PendingIntoValue> pendingIntoValues = new ArrayList<>();
        for (SysProductRecordDetentionRecord sysProductRecordDetentionRecord : sysProductRecordDetentionRecordList) {
            PendingIntoValue pendingIntoValue = new PendingIntoValue();
            Pending_into_date pending_into_date = new Pending_into_date();
            pending_into_date.setValue(sysProductRecordDetentionRecord.getProductDate());
            pendingIntoValue.setPending_into_date(pending_into_date);
            Pending_into_good_num pending_into_good_num = new Pending_into_good_num();
            pending_into_good_num.setValue(sysProductRecordDetentionRecord.getNum());
            pendingIntoValue.setPending_into_good_num(pending_into_good_num);
            pendingIntoValues.add(pendingIntoValue);
        }
        pending_into_num.setValue(pendingIntoValues);
        data.setPending_into_num(pending_into_num);


        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_BAD_RECORD.CREATE_TIME.between(productRecordInto.getActualStartTime(),productRecordInto.getActualEndTime()));
        queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
        queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productDate));
        queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.select(SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
        queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
        List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
        Base_bad base_bad = new Base_bad();
        List<Value1> value1List = new ArrayList<>();
        Process_bad process_bad = new Process_bad();
        List<ProcessBadValue> processBadValueList = new ArrayList<>();
        Manufactue_bad manufactue_bad = new Manufactue_bad();
        List<ManufactureBadValue> manFactureBadValueList = new ArrayList<>();
        int openBadNum = 0;
        int manufactureBadNum = 0;
        int processBadNum = 0;
        int productBadNum = 0;
        int totalBadNum = 0;
        for (SysBadRecord badRecord : badRecordList) {
            totalBadNum += badRecord.getNum();
            switch (badRecord.getReasonType()) {
                case "开机不良":
                    openBadNum += badRecord.getNum();
                    break;
                case "制程不良":
                    ManufactureBadValue manufactureBadValue = new ManufactureBadValue();
                    Manufacture_bad_reason manufacture_bad_reason = new Manufacture_bad_reason();
                    manufacture_bad_reason.setValue(badRecord.getReasonName());
                    manufactureBadValue.setManufactue_bad_reason(manufacture_bad_reason);

                    Manufacture_bad_num manufacture_bad_num = new Manufacture_bad_num();
                    manufacture_bad_num.setValue(badRecord.getNum());
                    manufactureBadValue.setManufactue_bad_num(manufacture_bad_num);
                    manFactureBadValueList.add(manufactureBadValue);
                    manufactureBadNum += badRecord.getNum();
                    break;

                case "加工不良":
                    processBadNum += badRecord.getNum();
                    ProcessBadValue processBadValue = new ProcessBadValue();
                    Process_bad_reason process_bad_reason = new Process_bad_reason();
                    process_bad_reason.setValue(badRecord.getReasonName());
                    processBadValue.setProcess_bad_reason(process_bad_reason);

                    Process_bad_num process_bad_num = new Process_bad_num();
                    process_bad_num.setValue(badRecord.getNum());
                    processBadValue.setProcess_bad_num(process_bad_num);
                    processBadValueList.add(processBadValue);
                    break;
                default:

                    Value1 value1 = new Value1();
                    Base_bad_reason base_bad_reason = new Base_bad_reason();
                    base_bad_reason.setValue(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")");
                    value1.setBase_bad_reason(base_bad_reason);
                    Base_bad_num base_bad_num = new Base_bad_num();
                    base_bad_num.setValue(badRecord.getNum());
                    value1.setBase_bad_num(base_bad_num);
                    value1List.add(value1);
                    break;
            }

        }
        Total_bad_num total_bad_num = new Total_bad_num();
        total_bad_num.setValue(totalBadNum);
        data.setTotal_bad_num(total_bad_num);

        base_bad.setValue(value1List);
        manufactue_bad.setValue(manFactureBadValueList);
        process_bad.setValue(processBadValueList);
        data.setBase_bad(base_bad);
        data.setManufactue_bad(manufactue_bad);
        data.setProcess_bad(process_bad);

        Aux_open_machine_bad aux_open_machine_bad = new Aux_open_machine_bad();
        aux_open_machine_bad.setValue(openBadNum);
        data.setAux_open_machine_bad(aux_open_machine_bad);

        Aux_process_bad aux_process_bad = new Aux_process_bad();
        aux_process_bad.setValue(manufactureBadNum);
        data.setAux_process_bad(aux_process_bad);

        Aux_product_bad aux_product_bad = new Aux_product_bad();
        aux_product_bad.setValue(productBadNum);
        data.setAux_product_bad(aux_product_bad);

        Aux_shift aux_shift = new Aux_shift();
        aux_shift.setValue(productRecordInto.getShiftCode());
        data.setAux_shift(aux_shift);

        Aux_effective_hours aux_effective_hours = new Aux_effective_hours();
        aux_effective_hours.setValue(hour - (productRecordInto.getAdjustMachineDuration() / 60));
        data.setAux_effective_hours(aux_effective_hours);

        Aux_product_name aux_product_name = new Aux_product_name();
        aux_product_name.setValue(productRecordInto.getProductName());
        data.setAux_product_name(aux_product_name);

        Today_into_num today_into_num = new Today_into_num();

        today_into_num.setValue(productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionToGoodNum() + productRecordInto.getWillProcessedToGoodNum());

        data.setToday_into_num(today_into_num);

        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_PLAN_ID.eq(productRecordInto.getWorkPlanId()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(productRecordInto.getMachineName()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_DATE.eq(fromDateToString("yyyy-MM-dd", productDate)));
        queryWrapper.select(distinct(SYS_PRODUCT_PEOPLE.JOB_NO), SYS_PRODUCT_PEOPLE.USER_NAME);
        log.error("员工：{}", queryWrapper.toSQL());
        List<SysProductPeople> productPeopleList = this.sysProductPeopleService.list(queryWrapper);
        log.error("ProductPeopleList:{}", JSONObject.toJSONString(productPeopleList));
        int i = 0;
        People people = new People();
        List<PeopleInfo> peopleInfoList = new ArrayList<>();
        StringJoiner userNameList = new StringJoiner(",");
        StringJoiner jobNoList = new StringJoiner(",");
        for (SysProductPeople productPeople : productPeopleList) {
            PeopleInfo peopleInfo = new PeopleInfo();
            i++;
            People_index people_index = new People_index();
            people_index.setValue(i);
            peopleInfo.setPeople_index(people_index);
            userNameList.add(productPeople.getUserName());
            jobNoList.add(productPeople.getJobNo());
            People_job_no people_job_no = new People_job_no();
            people_job_no.setValue(productPeople.getJobNo());
            peopleInfo.setPeople_job_no(people_job_no);

            People_user_name people_user_name = new People_user_name();
            people_user_name.setValue(productPeople.getUserName());
            peopleInfo.setPeople_user_name(people_user_name);
            peopleInfoList.add(peopleInfo);

        }
        people.setValue(peopleInfoList);
        data.setPeople(people);

        Remark_name remark_name = new Remark_name();
        remark_name.setValue(userNameList.toString());
        data.setRemark_name(remark_name);
        int peopleSize = productPeopleList.size();
        Aux_people_num aux_people_num = new Aux_people_num();
        aux_people_num.setValue(peopleSize);
        data.setAux_people_num(aux_people_num);

        People_num people_num = new People_num();
        people_num.setValue(String.valueOf(peopleSize));
        data.setPeople_num(people_num);

        Aux_product_time aux_product_time = new Aux_product_time();
        aux_product_time.setValue(productDate.getTime());
        data.setAux_product_time(aux_product_time);

        Line_manager_job_no line_manager_job_no = new Line_manager_job_no();
        line_manager_job_no.setValue(productRecordInto.getCreateUserId());
        data.setLine_manager_job_no(line_manager_job_no);

        Line_manager_name line_manager_name = new Line_manager_name();
        line_manager_name.setValue(productRecordInto.getCreateUserName());
        data.setLine_manager_name(line_manager_name);

        queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PLC.MACHINE_NAME.eq(productRecordInto.getMachineName()));
        queryWrapper.select(avg(SYS_PLC.AVERAGE_PERIOD).as("average_period"));
        queryWrapper.where(SYS_PLC.CREATE_TIME.between(productRecordShift.getActualStartTime(), productRecordInto.getActualEndTime()));
        SysPlc sysPlc = this.sysPlcService.getOne(queryWrapper);
        if (sysPlc != null) {
            Actual_cycle actual_cycle = new Actual_cycle();
            actual_cycle.setValue((int) ((double) sysPlc.getAveragePeriod() / 1000));
            data.setActual_cycle(actual_cycle);
        }
        productTodayDataSingle.setData(data);
        return productTodayDataSingle;
    }

    public ProductTodayDataSingle pushProductTodayDataSingle1(SysProductRecordInto productRecordInto) {

        log.error("入库数据：{}", JSONObject.toJSONString(productRecordInto));
        QueryWrapper queryWrapper = QueryWrapper.create();
        SysProductRecordShift productRecordShift = productRecordInto.getProductRecordShift();
        if(productRecordShift.getWorkPlan()==null){
            return null;
        }
        SysWorkPlan workPlan = productRecordShift.getWorkPlan();

        SysProductInfo productInfo = productRecordInto.getProductInfo();
        Date productDate = productRecordInto.getProductDate();
        String machineName = productRecordInto.getMachineName();
        log.error("机台");
        ProductTodayDataSingle productTodayDataSingle = new ProductTodayDataSingle();

        Data data = new Data();
        /*数据Key*/
        Key key = new Key();
        key.setValue(fromDateToString("yyyyMMdd", productDate) + machineName + workPlan.getMoldNo() + productRecordInto.getProductId() + productRecordInto.getShift() + productRecordInto.getShiftCode());
        data.setKey(key);

        /*员工姓名*/
        User_name user_name = new User_name();
        user_name.setValue(productRecordInto.getDingId());
        data.setUser_name(user_name);

        /*机台*/
        Machine_name machine_name = new Machine_name();
        machine_name.setValue(productRecordInto.getMachineName());
        data.setMachine_name(machine_name);

        /*模具*/
        Mold_name mold_name = new Mold_name();
        mold_name.setValue(workPlan.getMoldNo());
        data.setMold_name(mold_name);



        /*模穴数*/
        Mold_num mold_num = new Mold_num();
        mold_num.setValue((int) workPlan.getMoldNumber());
        data.setMold_num(mold_num);

        /*总数*/
        Total_count total_count = new Total_count();
        total_count.setValue(productRecordShift.getTotalCount());
        data.setTotal_count(total_count);


        Product_id product_id = new Product_id();
        product_id.setValue(productRecordInto.getProductId());
        data.setProduct_id(product_id);



        /*生产日期*/
        Product_date product_date = new Product_date();
        product_date.setValue(productDate);
        data.setProduct_date(product_date);

        /*开始时间*/
        Product_start_time product_start_time = new Product_start_time();
        product_start_time.setValue(productRecordShift.getActualStartTime());
        data.setProduct_start_time(product_start_time);

        /*结束时间*/
        Product_end_time product_end_time = new Product_end_time();
        product_end_time.setValue(productRecordShift.getActualEndTime());
        data.setProduct_end_time(product_end_time);
        long times = productRecordShift.getActualEndTime().getTime() - productRecordShift.getActualStartTime().getTime();
        double hours = times / (60 * 60 * 1000);
        BigDecimal bigDecimal = BigDecimal.valueOf(hours);
        double hour = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        double adjustTime=productRecordInto.getAdjustMachineDuration()/60;
        BigDecimal bigDecimal1 = BigDecimal.valueOf(adjustTime);
//        hour=hour-bigDecimal1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        if (hour >= 5) {
            hour = hour - 0.5;
        }
        hour=hour==0?11.5:hour;
        Actual_work_time_num actual_work_time_num = new Actual_work_time_num();
        actual_work_time_num.setValue(hour);
        data.setActual_work_time_num(actual_work_time_num);
        /*班别*/
        Shift shift = new Shift();
        shift.setValue(productRecordInto.getShiftCode());
        data.setShift(shift);

        Is_auto is_auto = new Is_auto();
        is_auto.setValue("全自动");
        data.setIs_auto(is_auto);
        log.error("产品");

//        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.PRODUCT_DATE.eq(productDate));
//        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
//        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.NUM.gt(0));
//        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.SHIFT.eq(productRecordInto.getShift()));
//        queryWrapper.select(SYS_PRODUCT_RECORD_DETENTION.PRODUCT_DATE, sum(SYS_PRODUCT_RECORD_DETENTION.NUM).as("num"), SYS_PRODUCT_RECORD_DETENTION.TYPE);
//        queryWrapper.groupBy(SYS_PRODUCT_RECORD_DETENTION.TYPE);
//        List<SysProductRecordDetention> productRecordDetentionList = this.sysProductRecordDetentionService.list(queryWrapper);
//        int detentionNum = 0;
//        for (SysProductRecordDetention productRecordDetention : productRecordDetentionList) {
//            detentionNum += productRecordDetention.getNum();
//        }
        int detentionNum=productRecordInto.getDetentionNum()+productRecordInto.getWillProcessedNum();
        Detention_num detention_num = new Detention_num();
        detention_num.setValue(detentionNum);
        data.setDetention_num(detention_num);
        log.error("input_quantity");
        Input_quantity input_quantity = new Input_quantity();
        if (productRecordShift.getTotalNumPeople() != null) {
            log.error("Q:{}", productRecordShift.getTotalNumPeople());
            input_quantity.setValue(productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionNum() + productRecordInto.getBadNumPeople() + productRecordInto.getWillProcessedNum() + productRecordInto.getNowTailBoxCount());

        }

        data.setInput_quantity(input_quantity);
        if (productInfo != null) {
            Customer_name customer_name = new Customer_name();
            customer_name.setValue(productInfo.getCustomerName());
            data.setCustomer_name(customer_name);

            Standard_recycle standard_recycle = new Standard_recycle();
            standard_recycle.setValue(productInfo.getStandardCycle().intValue());
            data.setStandard_recycle(standard_recycle);

            Machine_type_name machine_type_name = new Machine_type_name();
            machine_type_name.setValue(productInfo.getMachineType());
            data.setMachine_type_name(machine_type_name);

            log.error("机种");
            Machine_type_short_name machine_type_short_name = new Machine_type_short_name();
            machine_type_short_name.setValue(productInfo.getMachineType() + "-" + productInfo.getMachineItem());
            data.setMachine_type_short_name(machine_type_short_name);
            log.error("standard_num");
            int standardProductNum = (int) ((hour * 60 * 60 * workPlan.getMoldNumber()) / productInfo.getStandardCycle());
            Standard_num standard_num = new Standard_num();
            standard_num.setValue(standardProductNum);
            data.setStandard_num(standard_num);


            Aux_product_capacity aux_product_capacity = new Aux_product_capacity();
            int capacity = (int) ((60 * 60 * workPlan.getMoldNumber()) / productInfo.getStandardCycle());
            aux_product_capacity.setValue(capacity);
            data.setAux_product_capacity(aux_product_capacity);
            log.error("标准周期");
            double rate = 0;
            double rateN = 0;

            int handleInto = 0;

            handleInto = productRecordInto.getTotalHandleInto();


            if (standardProductNum > 0) {
                rate = (double) (productRecordInto.getGoodNumPeople() + productRecordInto.getBadNumPeople() + productRecordInto.getWillProcessedNum() + productRecordInto.getDetentionNum()) / standardProductNum;
                BigDecimal rateBigDecimal = new BigDecimal(rate);
                rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            }


            log.error("辅助信息");
            Aux_achievement_rate aux_achievement_rate = new Aux_achievement_rate();
            aux_achievement_rate.setValue(rateN);
            data.setAux_achievement_rate(aux_achievement_rate);

        }

        Good_num good_num = new Good_num();

        good_num.setValue(productRecordInto.getGoodNumPeople());


        data.setGood_num(good_num);


        log.error("辅助信息");
        Aux_bad_rate aux_bad_rate = new Aux_bad_rate();
        double rateN = 0;
        if (productRecordShift.getTotalNumPeople() != null) {
            if (productRecordShift.getTotalNumPeople() > 0) {
                double rate = (double) productRecordInto.getBadNumPeople() / productRecordShift.getTotalNumPeople();
                BigDecimal rateBigDecimal = new BigDecimal(rate);
                rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
        }

        aux_bad_rate.setValue(rateN);
        data.setAux_bad_rate(aux_bad_rate);

        /*当日尾箱*/
        Tail_box_num tail_box_num = new Tail_box_num();
        tail_box_num.setValue(productRecordShift.getNowTailBoxCount());
        data.setTail_box_num(tail_box_num);
        /*前日尾箱*/
        Last_tail_box_num last_tail_box_num = new Last_tail_box_num();
        last_tail_box_num.setValue(productRecordShift.getLastTailBoxCount());
        data.setLast_tail_box_num(last_tail_box_num);


        queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.PRODUCT_RECORD_INTO_ID.eq(productRecordInto.getId()));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.TYPE.eq("良品"));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.NUM.gt(0));
        queryWrapper.groupBy(SYS_PRODUCT_RECORD_DETENTION_RECORD.PRODUCT_DATE);
        List<SysProductRecordDetentionRecord> sysProductRecordDetentionRecordList = this.sysProductRecordDetentionRecordService.list(queryWrapper);
        Pending_into_num pending_into_num = new Pending_into_num();
        List<PendingIntoValue> pendingIntoValues = new ArrayList<>();
        for (SysProductRecordDetentionRecord sysProductRecordDetentionRecord : sysProductRecordDetentionRecordList) {
            PendingIntoValue pendingIntoValue = new PendingIntoValue();
            Pending_into_date pending_into_date = new Pending_into_date();
            pending_into_date.setValue(sysProductRecordDetentionRecord.getProductDate());
            pendingIntoValue.setPending_into_date(pending_into_date);
            Pending_into_good_num pending_into_good_num = new Pending_into_good_num();
            pending_into_good_num.setValue(sysProductRecordDetentionRecord.getNum());
            pendingIntoValue.setPending_into_good_num(pending_into_good_num);
            pendingIntoValues.add(pendingIntoValue);
        }
        pending_into_num.setValue(pendingIntoValues);
        data.setPending_into_num(pending_into_num);

        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_BAD_RECORD.CREATE_TIME.between(productRecordInto.getActualStartTime(),productRecordInto.getActualEndTime()));
        queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
        queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",productDate)));
        queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.select(SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
        queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
        List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
        log.error("BadRecordList:{}",JSONObject.toJSONString(badRecordList));
        Base_bad base_bad = new Base_bad();
        List<Value1> value1List = new ArrayList<>();
        Process_bad process_bad = new Process_bad();
        List<ProcessBadValue> processBadValueList = new ArrayList<>();
        Manufactue_bad manufactue_bad = new Manufactue_bad();
        List<ManufactureBadValue> manFactureBadValueList = new ArrayList<>();
        int openBadNum = 0;
        int manufactureBadNum = 0;
        int processBadNum = 0;
        int productBadNum = 0;
        int totalBadNum = 0;
        for (SysBadRecord badRecord : badRecordList) {
            totalBadNum += badRecord.getNum();
            switch (badRecord.getReasonType()) {
                case "开机不良":
                    openBadNum += badRecord.getNum();
                    break;
                case "制程不良":
                    ManufactureBadValue manufactureBadValue = new ManufactureBadValue();
                    Manufacture_bad_reason manufacture_bad_reason = new Manufacture_bad_reason();
                    manufacture_bad_reason.setValue(badRecord.getReasonName());
                    manufactureBadValue.setManufactue_bad_reason(manufacture_bad_reason);

                    Manufacture_bad_num manufacture_bad_num = new Manufacture_bad_num();
                    manufacture_bad_num.setValue(badRecord.getNum());
                    manufactureBadValue.setManufactue_bad_num(manufacture_bad_num);
                    manFactureBadValueList.add(manufactureBadValue);
                    manufactureBadNum += badRecord.getNum();
                    break;

                case "加工不良":
                    processBadNum += badRecord.getNum();
                    ProcessBadValue processBadValue = new ProcessBadValue();
                    Process_bad_reason process_bad_reason = new Process_bad_reason();
                    process_bad_reason.setValue(badRecord.getReasonName());
                    processBadValue.setProcess_bad_reason(process_bad_reason);

                    Process_bad_num process_bad_num = new Process_bad_num();
                    process_bad_num.setValue(badRecord.getNum());
                    processBadValue.setProcess_bad_num(process_bad_num);
                    processBadValueList.add(processBadValue);
                    break;
                default:

                    Value1 value1 = new Value1();
                    Base_bad_reason base_bad_reason = new Base_bad_reason();
                    base_bad_reason.setValue(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")");
                    value1.setBase_bad_reason(base_bad_reason);
                    Base_bad_num base_bad_num = new Base_bad_num();
                    base_bad_num.setValue(badRecord.getNum());
                    value1.setBase_bad_num(base_bad_num);
                    value1List.add(value1);
                    break;
            }

        }

        log.error("TotalBad:{}",totalBadNum);
        Total_bad_num total_bad_num = new Total_bad_num();
        total_bad_num.setValue(totalBadNum);
        data.setTotal_bad_num(total_bad_num);

        base_bad.setValue(value1List);
        manufactue_bad.setValue(manFactureBadValueList);
        process_bad.setValue(processBadValueList);
        data.setBase_bad(base_bad);
        data.setManufactue_bad(manufactue_bad);
        data.setProcess_bad(process_bad);

        Aux_open_machine_bad aux_open_machine_bad = new Aux_open_machine_bad();
        aux_open_machine_bad.setValue(openBadNum);
        data.setAux_open_machine_bad(aux_open_machine_bad);

        Aux_process_bad aux_process_bad = new Aux_process_bad();
        aux_process_bad.setValue(manufactureBadNum);
        data.setAux_process_bad(aux_process_bad);

        Aux_product_bad aux_product_bad = new Aux_product_bad();
        aux_product_bad.setValue(productBadNum);
        data.setAux_product_bad(aux_product_bad);

        Aux_shift aux_shift = new Aux_shift();
        aux_shift.setValue(productRecordInto.getShiftCode());
        data.setAux_shift(aux_shift);

        Aux_effective_hours aux_effective_hours = new Aux_effective_hours();
        aux_effective_hours.setValue(hour - (productRecordInto.getAdjustMachineDuration() / 60));
        data.setAux_effective_hours(aux_effective_hours);

        Aux_product_name aux_product_name = new Aux_product_name();
        aux_product_name.setValue(productRecordInto.getProductName());
        data.setAux_product_name(aux_product_name);

        Today_into_num today_into_num = new Today_into_num();

        today_into_num.setValue(productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionToGoodNum() + productRecordInto.getWillProcessedToGoodNum());


        data.setToday_into_num(today_into_num);

        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_TIME.ge(fromDateToString("yyyy-MM-dd HH:00:00",productRecordInto.getActualStartTime())));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_TIME.lt(productRecordInto.getActualEndTime()));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(productRecordShift.getMachineName()));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_PLAN_ID.eq(productRecordInto.getWorkPlanId()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(productRecordInto.getMachineName()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_DATE.eq(fromDateToString("yyyy-MM-dd", productDate)));
        queryWrapper.select(distinct(SYS_PRODUCT_PEOPLE.JOB_NO), SYS_PRODUCT_PEOPLE.USER_NAME);
        log.error("员工：{}", queryWrapper.toSQL());
        List<SysProductPeople> productPeopleList = this.sysProductPeopleService.list(queryWrapper);
        log.error("ProductPeopleList:{}", JSONObject.toJSONString(productPeopleList));
        int i = 0;
        People people = new People();
        List<PeopleInfo> peopleInfoList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        StringJoiner userNameList = new StringJoiner(",");
        StringJoiner jobNoList = new StringJoiner(",");
        for (SysProductPeople productPeople : productPeopleList) {
            nameList.add(productPeople.getUserName());
            PeopleInfo peopleInfo = new PeopleInfo();
            i++;
            People_index people_index = new People_index();
            people_index.setValue(i);
            peopleInfo.setPeople_index(people_index);
            userNameList.add(productPeople.getUserName());
            jobNoList.add(productPeople.getJobNo());
            People_job_no people_job_no = new People_job_no();
            people_job_no.setValue(productPeople.getJobNo());
            peopleInfo.setPeople_job_no(people_job_no);

            People_user_name people_user_name = new People_user_name();
            people_user_name.setValue(productPeople.getUserName());
            peopleInfo.setPeople_user_name(people_user_name);
            peopleInfoList.add(peopleInfo);

        }
        people.setValue(peopleInfoList);
        data.setPeople(people);

        User_name1 user_name1 = new User_name1();
        user_name1.setValue(nameList);
        data.setUser_name1(user_name1);


        Remark_name remark_name = new Remark_name();
        remark_name.setValue(userNameList.toString());
        data.setRemark_name(remark_name);
        int peopleSize = productPeopleList.size();
        Aux_people_num aux_people_num = new Aux_people_num();
        aux_people_num.setValue(peopleSize);
        data.setAux_people_num(aux_people_num);

        People_num people_num = new People_num();
        people_num.setValue(String.valueOf(peopleSize));
        data.setPeople_num(people_num);

        Aux_product_time aux_product_time = new Aux_product_time();
        aux_product_time.setValue(productDate.getTime());
        data.setAux_product_time(aux_product_time);

        Line_manager_job_no line_manager_job_no = new Line_manager_job_no();
        line_manager_job_no.setValue(productRecordInto.getCreateUserId());
        data.setLine_manager_job_no(line_manager_job_no);

        Line_manager_name line_manager_name = new Line_manager_name();
        line_manager_name.setValue(productRecordInto.getCreateUserName());
        data.setLine_manager_name(line_manager_name);

//        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PLC.MACHINE_NAME.eq(productRecordInto.getMachineName()));
//        queryWrapper.select(avg(SYS_PLC.AVERAGE_PERIOD).as("average_period"));
//        queryWrapper.where(SYS_PLC.CREATE_TIME.between(productRecordShift.getActualStartTime(), productRecordShift.getActualEndTime()));
//        SysPlc sysPlc = this.sysPlcService.getOne(queryWrapper);
        int total=productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionNum() + productRecordInto.getBadNumPeople() + productRecordInto.getWillProcessedNum() + productRecordInto.getNowTailBoxCount();

        if (total>0) {
            Actual_cycle actual_cycle = new Actual_cycle();
            int actual= (int) (hour*60*60/(total/workPlan.getMoldNumber()));
            if(actual>100){
                actual=35;
            }
            actual_cycle.setValue(actual);
            data.setActual_cycle(actual_cycle);
        }
        Remark remark = new Remark();
        remark.setValue(productRecordInto.getSummary());
        data.setRemark(remark);
        Creator creator = new Creator();
        creator.setValue(productRecordInto.getDingId());
        data.setCreator(creator);
        productTodayDataSingle.setData(data);
        return productTodayDataSingle;
    }

    public UpdateProductTodayDataSingle updteProductTodayDataSingle1(SysProductRecordInto productRecordInto) {
//        RelationManager.addExtraConditionParam("productDate", "2024-03-13");
//        RelationManager.addExtraConditionParam("status", 2);
        QueryWrapper queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PRODUCT_RECORD_INTO.ID.eq("b701860b-da94-4851-8f35-c707706102b0"));
//        SysProductRecordInto productRecordInto = this.sysProductRecordIntoService.getMapper().selectOneWithRelationsByQuery(queryWrapper);
        SysProductRecordShift productRecordShift = productRecordInto.getProductRecordShift();
        SysWorkPlan workPlan = productRecordShift.getWorkPlan();
        SysProductInfo productInfo = productRecordInto.getProductInfo();
        log.error("ProductRecord:{}", JSONObject.toJSONString(productRecordShift));
        log.error("ProductInfo:{}", JSONObject.toJSONString(productInfo));
        log.error("工单：{}", JSONObject.toJSONString(workPlan));
        Date productDate = productRecordInto.getProductDate();
        String machineName = productRecordInto.getMachineName();
        log.error("机台");
        UpdateProductTodayDataSingle updateProductTodayDataSingle = new UpdateProductTodayDataSingle();

        Data data = new Data();
        /*数据Key*/
        Key key = new Key();
        key.setValue(fromDateToString("yyyyMMdd", productDate) + machineName + workPlan.getMoldNo() + productRecordInto.getProductId() + productRecordInto.getShift() + productRecordInto.getShiftCode());
        data.setKey(key);

        /*员工姓名*/
        User_name user_name = new User_name();
        user_name.setValue(productRecordInto.getDingId());
        data.setUser_name(user_name);

        /*机台*/
        Machine_name machine_name = new Machine_name();
        machine_name.setValue(productRecordInto.getMachineName());
        data.setMachine_name(machine_name);

        /*模具*/
        Mold_name mold_name = new Mold_name();
        mold_name.setValue(workPlan.getMoldNo());
        data.setMold_name(mold_name);



        /*模穴数*/
        Mold_num mold_num = new Mold_num();
        mold_num.setValue((int) workPlan.getMoldNumber());
        data.setMold_num(mold_num);

        /*总数*/
        Total_count total_count = new Total_count();
        total_count.setValue(productRecordShift.getTotalCount());
        data.setTotal_count(total_count);


        Product_id product_id = new Product_id();
        product_id.setValue(productRecordShift.getProductId());
        data.setProduct_id(product_id);



        /*生产日期*/
        Product_date product_date = new Product_date();
        product_date.setValue(productDate);
        data.setProduct_date(product_date);

        /*开始时间*/
        Product_start_time product_start_time = new Product_start_time();
        product_start_time.setValue(productRecordShift.getActualStartTime());
        data.setProduct_start_time(product_start_time);

        /*结束时间*/
        Product_end_time product_end_time = new Product_end_time();
        product_end_time.setValue(productRecordShift.getActualEndTime());
        data.setProduct_end_time(product_end_time);
        long times = productRecordShift.getActualEndTime().getTime() - productRecordShift.getActualStartTime().getTime();
        double hours = times / (60 * 60 * 1000);
        BigDecimal bigDecimal = BigDecimal.valueOf(hours);
        double hour = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        double adjustTime=productRecordInto.getAdjustMachineDuration()/60;
        BigDecimal bigDecimal1 = BigDecimal.valueOf(adjustTime);
//        hour=hour-bigDecimal1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        if (hour >= 5) {
            hour = hour - 0.5;
        }
        hour=hour==0?11.5:hour;
        Actual_work_time_num actual_work_time_num = new Actual_work_time_num();
        actual_work_time_num.setValue(hour);
        data.setActual_work_time_num(actual_work_time_num);
        /*班别*/
        Shift shift = new Shift();
        shift.setValue(productRecordInto.getShiftCode());
        data.setShift(shift);

        Is_auto is_auto = new Is_auto();
        is_auto.setValue("全自动");
        data.setIs_auto(is_auto);
        log.error("产品");

        Input_quantity input_quantity = new Input_quantity();
        if (productRecordShift.getTotalNumPeople() != null) {
            input_quantity.setValue(productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionNum() + productRecordInto.getBadNumPeople() + productRecordInto.getWillProcessedNum() + productRecordInto.getNowTailBoxCount());
        }
        data.setInput_quantity(input_quantity);
        if (productInfo != null) {
            Customer_name customer_name = new Customer_name();
            customer_name.setValue(productInfo.getCustomerName());
            data.setCustomer_name(customer_name);

            Standard_recycle standard_recycle = new Standard_recycle();
            standard_recycle.setValue(productInfo.getStandardCycle().intValue());
            data.setStandard_recycle(standard_recycle);

            Machine_type_name machine_type_name = new Machine_type_name();
            machine_type_name.setValue(productInfo.getMachineType());
            data.setMachine_type_name(machine_type_name);

            log.error("机种");
            Machine_type_short_name machine_type_short_name = new Machine_type_short_name();
            machine_type_short_name.setValue(productInfo.getMachineType() + "-" + productInfo.getMachineItem());
            data.setMachine_type_short_name(machine_type_short_name);

            int standardProductNum = (int) ((hour * 60 * 60 * workPlan.getMoldNumber()) / productInfo.getStandardCycle());
            log.error("标准数量：{}", standardProductNum);
            Standard_num standard_num = new Standard_num();
            standard_num.setValue(standardProductNum);
            data.setStandard_num(standard_num);



            Aux_product_capacity aux_product_capacity = new Aux_product_capacity();
            int capacity = (int) ((60 * 60 * workPlan.getMoldNumber()) / productInfo.getStandardCycle());
            aux_product_capacity.setValue(capacity);
            data.setAux_product_capacity(aux_product_capacity);
            log.error("标准周期");
            double rate = 0;
            double rateN = 0;
            if (standardProductNum > 0) {
                rate = (double) (productRecordInto.getGoodNumPeople() + productRecordInto.getBadNumPeople() + productRecordInto.getWillProcessedNum() + productRecordInto.getDetentionNum() + productRecordInto.getNowTailBoxCount()) / standardProductNum;
                BigDecimal rateBigDecimal = new BigDecimal(rate);
                rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
            Aux_achievement_rate aux_achievement_rate = new Aux_achievement_rate();
            aux_achievement_rate.setValue(rateN);
            data.setAux_achievement_rate(aux_achievement_rate);

        }

        Good_num good_num = new Good_num();

        good_num.setValue(productRecordInto.getGoodNumPeople());


        data.setGood_num(good_num);


        log.error("辅助信息");
        Aux_bad_rate aux_bad_rate = new Aux_bad_rate();
        double rateN = 0;
        if (productRecordShift.getTotalNumPeople() != null) {
            if (productRecordShift.getTotalNumPeople() > 0) {
                double rate = (double) productRecordInto.getBadNumPeople() / productRecordShift.getTotalNumPeople();
                BigDecimal rateBigDecimal = new BigDecimal(rate);
                rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
        }

        aux_bad_rate.setValue(rateN);
        data.setAux_bad_rate(aux_bad_rate);

        /*当日尾箱*/
        Tail_box_num tail_box_num = new Tail_box_num();
        tail_box_num.setValue(productRecordShift.getNowTailBoxCount());
        data.setTail_box_num(tail_box_num);
        /*前日尾箱*/
        Last_tail_box_num last_tail_box_num = new Last_tail_box_num();
        last_tail_box_num.setValue(productRecordShift.getLastTailBoxCount());
        data.setLast_tail_box_num(last_tail_box_num);

//        Detention_num detention_num=new Detention_num();
//        detention_num.setValue(productRecordShift.);

        queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.PRODUCT_DATE.eq(productDate));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.NUM.gt(0));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.select(SYS_PRODUCT_RECORD_DETENTION.PRODUCT_DATE, sum(SYS_PRODUCT_RECORD_DETENTION.NUM).as("num"), SYS_PRODUCT_RECORD_DETENTION.TYPE);
        queryWrapper.groupBy(SYS_PRODUCT_RECORD_DETENTION.TYPE);
        List<SysProductRecordDetention> productRecordDetentionList = this.sysProductRecordDetentionService.list(queryWrapper);
        int detentionNum = 0;
        for (SysProductRecordDetention productRecordDetention : productRecordDetentionList) {
            detentionNum += productRecordDetention.getNum();
        }
        Detention_num detention_num = new Detention_num();
        detention_num.setValue(detentionNum);
        data.setDetention_num(detention_num);


        queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.PRODUCT_RECORD_INTO_ID.eq(productRecordInto.getId()));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.TYPE.eq("良品"));
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.NUM.gt(0));
        queryWrapper.groupBy(SYS_PRODUCT_RECORD_DETENTION_RECORD.PRODUCT_DATE);
        List<SysProductRecordDetentionRecord> sysProductRecordDetentionRecordList = this.sysProductRecordDetentionRecordService.list(queryWrapper);
        Pending_into_num pending_into_num = new Pending_into_num();
        List<PendingIntoValue> pendingIntoValues = new ArrayList<>();
        for (SysProductRecordDetentionRecord sysProductRecordDetentionRecord : sysProductRecordDetentionRecordList) {
            PendingIntoValue pendingIntoValue = new PendingIntoValue();
            Pending_into_date pending_into_date = new Pending_into_date();
            pending_into_date.setValue(sysProductRecordDetentionRecord.getProductDate());
            pendingIntoValue.setPending_into_date(pending_into_date);
            Pending_into_good_num pending_into_good_num = new Pending_into_good_num();
            pending_into_good_num.setValue(sysProductRecordDetentionRecord.getNum());
            pendingIntoValue.setPending_into_good_num(pending_into_good_num);
            pendingIntoValues.add(pendingIntoValue);
        }
        pending_into_num.setValue(pendingIntoValues);
        data.setPending_into_num(pending_into_num);

        queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_BAD_RECORD.CREATE_TIME.between(productRecordInto.getActualStartTime(),productRecordInto.getActualEndTime()));
        queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
        queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",productDate)));
        queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.select(SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
        queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
        log.error("SysPSQL:{}",queryWrapper.toSQL());
        List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
        log.error("BadRecordList:{}",JSONObject.toJSONString(badRecordList));
        Base_bad base_bad = new Base_bad();
        List<Value1> value1List = new ArrayList<>();
        Process_bad process_bad = new Process_bad();
        List<ProcessBadValue> processBadValueList = new ArrayList<>();
        Manufactue_bad manufactue_bad = new Manufactue_bad();
        List<ManufactureBadValue> manFactureBadValueList = new ArrayList<>();
        int openBadNum = 0;
        int manufactureBadNum = 0;
        int processBadNum = 0;
        int productBadNum = 0;
        int totalBadNum = 0;
        for (SysBadRecord badRecord : badRecordList) {
            totalBadNum += badRecord.getNum();
            switch (badRecord.getReasonType()) {
                case "开机不良":
                    openBadNum += badRecord.getNum();
                    break;
                case "制程不良":
                    ManufactureBadValue manufactureBadValue = new ManufactureBadValue();
                    Manufacture_bad_reason manufacture_bad_reason = new Manufacture_bad_reason();
                    manufacture_bad_reason.setValue(badRecord.getReasonName());
                    manufactureBadValue.setManufactue_bad_reason(manufacture_bad_reason);

                    Manufacture_bad_num manufacture_bad_num = new Manufacture_bad_num();
                    manufacture_bad_num.setValue(badRecord.getNum());
                    manufactureBadValue.setManufactue_bad_num(manufacture_bad_num);
                    manFactureBadValueList.add(manufactureBadValue);
                    manufactureBadNum += badRecord.getNum();
                    break;

                case "加工不良":
                    processBadNum += badRecord.getNum();
                    ProcessBadValue processBadValue = new ProcessBadValue();
                    Process_bad_reason process_bad_reason = new Process_bad_reason();
                    process_bad_reason.setValue(badRecord.getReasonName());
                    processBadValue.setProcess_bad_reason(process_bad_reason);

                    Process_bad_num process_bad_num = new Process_bad_num();
                    process_bad_num.setValue(badRecord.getNum());
                    processBadValue.setProcess_bad_num(process_bad_num);
                    processBadValueList.add(processBadValue);
                    break;
                default:

                    Value1 value1 = new Value1();
                    Base_bad_reason base_bad_reason = new Base_bad_reason();
                    base_bad_reason.setValue(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")");
                    value1.setBase_bad_reason(base_bad_reason);

                    Base_bad_num base_bad_num = new Base_bad_num();
                    base_bad_num.setValue(badRecord.getNum());
                    value1.setBase_bad_num(base_bad_num);
                    value1List.add(value1);
                    break;
            }

        }
        Total_bad_num total_bad_num = new Total_bad_num();
        total_bad_num.setValue(totalBadNum);
        data.setTotal_bad_num(total_bad_num);

        base_bad.setValue(value1List);
        manufactue_bad.setValue(manFactureBadValueList);
        process_bad.setValue(processBadValueList);
        data.setBase_bad(base_bad);
        data.setManufactue_bad(manufactue_bad);
        data.setProcess_bad(process_bad);

        Aux_open_machine_bad aux_open_machine_bad = new Aux_open_machine_bad();
        aux_open_machine_bad.setValue(openBadNum);
        data.setAux_open_machine_bad(aux_open_machine_bad);

        Aux_process_bad aux_process_bad = new Aux_process_bad();
        aux_process_bad.setValue(manufactureBadNum);
        data.setAux_process_bad(aux_process_bad);

        Aux_product_bad aux_product_bad = new Aux_product_bad();
        aux_product_bad.setValue(productBadNum);
        data.setAux_product_bad(aux_product_bad);

        Aux_shift aux_shift = new Aux_shift();
        aux_shift.setValue(productRecordInto.getShiftCode());
        data.setAux_shift(aux_shift);

        Aux_effective_hours aux_effective_hours = new Aux_effective_hours();
        aux_effective_hours.setValue(hour - (productRecordInto.getAdjustMachineDuration() / 60));
        data.setAux_effective_hours(aux_effective_hours);

        Aux_product_name aux_product_name = new Aux_product_name();
        aux_product_name.setValue(productRecordInto.getProductName());
        data.setAux_product_name(aux_product_name);

        Today_into_num today_into_num = new Today_into_num();

        today_into_num.setValue(productRecordInto.getGoodNumPeople() + productRecordInto.getDetentionToGoodNum() + productRecordInto.getWillProcessedToGoodNum());

        data.setToday_into_num(today_into_num);

        queryWrapper = QueryWrapper.create();
        log.error("时间：{}", productRecordShift.getActualStartTime());
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_TIME.ge(fromDateToString("yyyy-MM-dd hh:00:00",productRecordInto.getActualStartTime())));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_TIME.lt(productRecordInto.getActualEndTime()));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_PLAN_ID.eq(productRecordInto.getWorkPlanId()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(productRecordInto.getMachineName()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.SHIFT.eq(productRecordInto.getShift()));
        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_DATE.eq(fromDateToString("yyyy-MM-dd", productDate)));
        queryWrapper.select(distinct(SYS_PRODUCT_PEOPLE.JOB_NO), SYS_PRODUCT_PEOPLE.USER_NAME);
        log.error("员工：{}", queryWrapper.toSQL());
        List<SysProductPeople> productPeopleList = this.sysProductPeopleService.list(queryWrapper);
        log.error("ProductPeopleList:{}", JSONObject.toJSONString(productPeopleList));
        int i = 0;
        People people = new People();
        List<PeopleInfo> peopleInfoList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        StringJoiner userNameList = new StringJoiner(",");
        StringJoiner jobNoList = new StringJoiner(",");
        for (SysProductPeople productPeople : productPeopleList) {
            nameList.add(productPeople.getUserName());
            PeopleInfo peopleInfo = new PeopleInfo();
            i++;
            People_index people_index = new People_index();
            people_index.setValue(i);
            peopleInfo.setPeople_index(people_index);
            userNameList.add(productPeople.getUserName());
            jobNoList.add(productPeople.getJobNo());
            People_job_no people_job_no = new People_job_no();
            people_job_no.setValue(productPeople.getJobNo());
            peopleInfo.setPeople_job_no(people_job_no);

            People_user_name people_user_name = new People_user_name();
            people_user_name.setValue(productPeople.getUserName());
            peopleInfo.setPeople_user_name(people_user_name);
            peopleInfoList.add(peopleInfo);

        }
        people.setValue(peopleInfoList);
        data.setPeople(people);

        User_name1 user_name1 = new User_name1();
        user_name1.setValue(nameList);
        data.setUser_name1(user_name1);


        Remark_name remark_name = new Remark_name();
        remark_name.setValue(userNameList.toString());
        data.setRemark_name(remark_name);
        int peopleSize = productPeopleList.size();
        Aux_people_num aux_people_num = new Aux_people_num();
        aux_people_num.setValue(peopleSize);
        data.setAux_people_num(aux_people_num);

        People_num people_num = new People_num();
        people_num.setValue(String.valueOf(peopleSize));
        data.setPeople_num(people_num);

        Aux_product_time aux_product_time = new Aux_product_time();
        aux_product_time.setValue(productDate.getTime());
        data.setAux_product_time(aux_product_time);

        Line_manager_job_no line_manager_job_no = new Line_manager_job_no();
        line_manager_job_no.setValue(productRecordInto.getCreateUserId());
        data.setLine_manager_job_no(line_manager_job_no);

        Line_manager_name line_manager_name = new Line_manager_name();
        line_manager_name.setValue(productRecordInto.getCreateUserName());
        data.setLine_manager_name(line_manager_name);


        Remark remark = new Remark();
        remark.setValue(productRecordInto.getSummary());
        data.setRemark(remark);

        Creator creator = new Creator();
        creator.setValue(productRecordInto.getDingId());
        data.setCreator(creator);
        updateProductTodayDataSingle.setData(data);
        return updateProductTodayDataSingle;
    }

}
